package day0727API;

public class Test10 {
    public static void main(String[] args) {
        Object o=new Object();
        Runnable r1=() -> {
            int count = 0;
            synchronized (o) {
                while (true) {
                    try{
                        count++;
                        System.out.println(Thread.currentThread().getName() + ":"+count);
                        Thread.sleep(1000);
                        if (count > 2) {
                            try {
                                o.notifyAll();
                                count = 1;
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }


            }
        };
        new Thread(r1, "线程1").start();



        new Thread(() -> {
            int count = 0;
            synchronized (o) {
                while (true) {
                    count++;
                    System.out.println(Thread.currentThread().getName() + ":"+count);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (count > 2) {
                        try {
                            o.notifyAll();
                            count = 1;
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }

            }
        }, "线程2").start();
    }
}

